<?php

/**
 * pracownicy actions.
 *
 * @package    testyZlecenia
 * @subpackage pracownicy
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class pracownicyActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->pracownicys = Doctrine::getTable('Pracownicy')
      ->createQuery('a')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->pracownicy = Doctrine::getTable('Pracownicy')->find(array($request->getParameter('id'),
                                             $request->getParameter('email')));
    $this->forward404Unless($this->pracownicy);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new PracownicyForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new PracownicyForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($pracownicy = Doctrine::getTable('Pracownicy')->find(array($request->getParameter('id'),
                       $request->getParameter('email'))), sprintf('Object pracownicy does not exist (%s).', $request->getParameter('id'),
                       $request->getParameter('email')));
    $this->form = new PracownicyForm($pracownicy);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($pracownicy = Doctrine::getTable('Pracownicy')->find(array($request->getParameter('id'),
                       $request->getParameter('email'))), sprintf('Object pracownicy does not exist (%s).', $request->getParameter('id'),
                       $request->getParameter('email')));
    $this->form = new PracownicyForm($pracownicy);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($pracownicy = Doctrine::getTable('Pracownicy')->find(array($request->getParameter('id'),
                       $request->getParameter('email'))), sprintf('Object pracownicy does not exist (%s).', $request->getParameter('id'),
                       $request->getParameter('email')));
    $pracownicy->delete();

    $this->redirect('pracownicy/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $pracownicy = $form->save();

      $this->redirect('pracownicy/edit?id='.$pracownicy->getId().'&email='.$pracownicy->getEmail());
    }
  }
}
